#delimit;
clear all;

/*Replication file for De Mel, S., McIntosh, C., & Woodruff, C. (2013). 
Deposit collecting: Unbundling the role of frequency, salience, and habit formation in generating savings. The American Economic Review, 103(3), 387-392*/
	

global savings " nsb_deposits nsb_withdrawals  mtransactions msavings_bank  msavings_nonbank msavings_total  ";		

/*PUT DIRECTORY HERE:*/
cd "";
use "sri_lanka_unbundling_data", clear;


/*defining dummies to be used for testing specific hypotheses:*/
/*Name is a dummy giving the relevant treatment and counterfactual as groups, _t is the treatment dummy that switches on.*/
/*Note that these dummies are created in negative; because the treatment effect of weekly home to biweekly home is decreasing salience,,
	salience itself is measured as the treatment effect of STAYING in the weekly home where the biweekly home is the counterfactual*/
/*two ways of measuring frequency; one is comparing weekly home to biweekly home, the other is comparing C to weekly box to C to biweekly box*/
gen frequency = (tweak=="Weekly Home" | tweak=="T to Biweekly Home");
gen frequency_t = (tweak=="Weekly Home" & round>=12);
gen frequency_box = (tweak=="C to Weekly Box" | tweak=="C to Biweekly Box");
gen frequency_box_t = (tweak=="C to Weekly Box" & round>=12);
gen salience = (tweak=="Weekly Home" | tweak=="T to Weekly Box");
gen salience_t = (tweak=="Weekly Home" & round>=12);
/*The habit formation dummy (T to weekly Box versus C to weekly Box) is harder to analyze because you have a raw treatment
	effect in rounds 6-11 from the basic treatment difference, and then they switch to the same treatment.  So, the easiest way to
	deal with this is simply drop the period in which their treatment status differed and examine whether these two groups 
	(that were the same in rounds 2-5) look different once they get to the same treatment status via a different route.*/
gen habit = (tweak=="C to Weekly Box" | tweak=="T to Weekly Box");
replace habit = 0 if (round>=6 & round<=11);
gen habit_t = (tweak=="T to Weekly Box" & round>=12);




gen CtoBox = 0;
replace CtoBox = 1 if (tweak=="Control" | tweak=="C to Weekly Box" | tweak=="C to Biweekly Box");
gen CtoBox_t = (tweak=="C to Weekly Box" | tweak=="C to Biweekly Box") & round>=12 & round!=.;


/*Interactions to measure survey effects:*/
foreach x in treated tw_treat_biweekly_home tw_treat_weekly_box  tw_control_weekly_box tw_control_biweekly_box frequency_t frequency_box_t salience_t habit_t  CtoBox_t {;
	gen m_`x' = monthly * `x';
	};
	
gen post_tweak=0;
replace post_tweak=1 if round>=12 & round!=.; 

/*Dropping those who attrited before they were assigned treatment:*/
drop if treat==.;


/*Calculating baseline means, doing the sample demeaning, and then creating interaction variables:*/
foreach x in $interactions{;
	egen b_`x' = mean(`x') if round>=1 & round<=4, by(id);
	egen b2_`x' = mean(b_`x'), by(id);
	egen b3_`x' = mean(b2_`x');
	gen bx_`x' = b2_`x' - b3_`x';
	gen frq_`x' = frequency_t * bx_`x';
	gen frqbox_`x' = frequency_box_t * bx_`x';
	gen sln_`x' = salience_t * bx_`x';
	gen hbt_`x' = habit_t * bx_`x';
	gen postx_`x' = post_tweak * bx_`x';
	drop b_`x' b2_`x' b3_`x';
	};

gen tweak_actual = tweak;
replace tweak_actual = "Weekly Home" if (tweak=="T to Biweekly Home" | tweak=="T to Weekly Box") & round<12;
replace tweak_actual = "Control" if (tweak=="C to Biweekly Box" | tweak=="C to Weekly Box") & round<12;
	
/*Interaction to look at duration effects:*/
gen timetotweak = round-11;
replace timetotweak=0 if timetotweak<0;
foreach x in frequency_t salience_t habit_t {;
	gen time_`x' = timetotweak * `x';
	};

	
/*Setting the NSB institutional data to zero for missings so as to analyze treatment/control differences:*/	
foreach x in nsb_mtransactions nsb_deposits nsb_withdrawals {;
		gen `x'_miss = `x';
		replace `x'_miss = . if `x'_miss==0;
		replace `x'=0 if `x'==.;
		};

	
		
/*trimming DVs at 98%, overwriting variables:*/
foreach x in    $savings $welfare {;
	egen p98_`x' = pctile(`x'), p(98);
	replace `x'=p98_`x' if `x'>p98_`x' & `x'!=.;
	};
	

	
/*generating indicator variables for each round AND SURVEY TYPE (so that the monthly and quarterly samples in the same round get different dummies:*/
forvalues x=3/18 {;
	gen monthdum_m`x' = 1 if (round==`x' & monthly==1);
	replace monthdum_m`x' = 0 if monthdum_m`x'==.;
	};
foreach x in 3 4 7 10 13 16 17 18 {;	
	gen monthdum_q`x' = 1 if (round==`x' & monthly==0);
	replace monthdum_q`x' = 0 if monthdum_q`x'==.;
	};
	






STOP;


/*TABLE 1:*/
/*IMPACT ANALYSIS OF ALL OF THE TWEAKS TOGETHER:  (note that you need the whole, tweaked sample to run this code:)*/
/*Also note that the control tweaks are measured relative to the control outcome, while the treatment tweaks are relative to the weekly home treatment*/	
#delimit;	
xtreg nsb_deposits treated monthdum_* if round>1 & panel==1 , i(id) fe cluster(id);
	outreg2  treated using income, se replace title("RESET");
foreach x in $savings {;
	xtreg `x' treated tw_treat_biweekly_home tw_treat_weekly_box  tw_control_weekly_box tw_control_biweekly_box 
		monthdum_* if round>1 & panel==1 , i(id) fe cluster(id);
	outreg2 treated tw_treat_biweekly_home tw_treat_weekly_box  tw_control_weekly_box tw_control_biweekly_box  using income, se  append;
	};
sum $savings if (tweak=="Control" & round>1 & panel==1);



/*TABLE 2 PANEL A:*/
/*Impact of FREQUENCY OF HOME VISITS, looking at keeping weekly with the switch to biweekly as the counterfactual:*/
#delimit;
clear results;
xtreg mtransactions frequency_t monthdum_* if round>1 & panel==1, i(id) fe cluster(id);
	outreg2  frequency_t using income, se  replace;
foreach x in $savings {;
	egen m=mean(`x') if (treated==1 & tweak=="Weekly Home");
	local N=m;
	xtreg `x' frequency_t monthdum_* if round>=6 & panel==1 & frequency==1, i(id) fe cluster(id);
	outreg2  frequency_t using income, se  append;
	cap drop m;
	};
sum  $savings if (treated==1 & tweak=="Weekly Home" & round>=6);
	

/*TABLE 2 PANEL B:*/
/*Impact of SALIENCE of savings, looking at keeping  weekly home visits with the switch to weekly box as the counterfactual:*/
#delimit;
xtreg mtransactions salience_t monthdum_* if round>=6 & panel==1 & salience==1, i(id) fe cluster(id);
	outreg2  salience_t using income, se  replace;
foreach x in $savings {;
	cap egen m=mean(`x') if treated==0;
	local M=m;
	xtreg `x' salience_t monthdum_* if round>=6 & panel==1 & salience==1 & round!=12, i(id) fe cluster(id);
	outreg2  salience_t using income, se addstat(mean in control, `M') append;
	};
	

/*TABLE 2 PANEL C:*/
/*Impact of HABIT FORMATION in savings; comparing the control to weekly box to the treatment to weekly box groups:*/
#delimit;
xtreg mtransactions habit_t monthdum_* if round>1 & panel==1, i(id) fe cluster(id);
	outreg2  salience_t using income, se  replace;
foreach x in $savings {;
	cap egen m=mean(`x') if treated==0;
	local M=m;
	xtreg `x' habit_t monthdum_* if round>=2 & panel==1 & habit==1, i(id) fe cluster(id);
	outreg2  habit_t using income, se addstat(mean in control, `M') append;
	};
		
		
		
